package resource

import (
	"gorm.io/gorm"
	"mallgo/internal/common/response"
	modelRequest "mallgo/internal/resources/product/api/domain/request"
	modelResponse "mallgo/internal/resources/product/api/domain/response"
)

type ProductCatalogResource struct {
	db *gorm.DB
}

func NewProductCatalogResource(db *gorm.DB) *ProductCatalogResource {
	return &ProductCatalogResource{db: db}
}

func (r *ProductCatalogResource) Create(req *modelRequest.ProductCatalogRequest) *response.ResponseObject {
	tx := r.db.Begin()
	productCatalog := modelRequest.ConvertProductCatalogModel(req)
	if err := tx.Create(&productCatalog).Error; err != nil {
		tx.Rollback()
		return response.Error(500, "创建失败")
	}
	tx.Commit()
	return response.Success(modelResponse.ConvertProductCatalogResponse(productCatalog))
}
